/*

The contents of this web application are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this web application except in compliance with the License. 
You may obtain a copy of the License at http://www.mozilla.org/MPL/.

Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
See the License for the specific language governing rights and limitations under the License.

The Original Code is owned by and the Initial Developer of the Original Code is Composite A/S (Danish business reg.no. 21744409). All Rights Reserved

Section 11 of the License is EXPRESSLY amended to include a provision stating that any dispute, including but not limited to disputes related to the enforcement of the License, to which Composite A/S as owner of the Original Code, as Initial Developer or in any other role, becomes a part to shall be governed by Danish law and be initiated before the Copenhagen City Court ("K�benhavns Byret")
                        
*/

DialogToolBarBinding.prototype = new ToolBarBinding;
DialogToolBarBinding.prototype.constructor = DialogToolBarBinding;
DialogToolBarBinding.superclass = ToolBarBinding.prototype;

/**
 * @class
 * TODO: move this file somewhere else...
 */
function DialogToolBarBinding () {

	/**
	 * @type {SystemLogger}
	 */
	this.logger = SystemLogger.getLogger ( "DialogToolBarBinding" );
	
	/**
	 * @type {List<ClickButtonBinding>}
	 */
	this._buttons = null;
	
	/**
	 * @type {ClickButtonBinding}
	 */
	this._defaultButton = null;
	
	/**
	 * @type {ClickButtonBinding}
	 */
	this._focusedButton = null;
	
	/** 
	 * Listening for ENTER keypress?
	 * @type {boolean}
	 */
	this._isListening = false;
	
	/**
	 * ALLOW focuscrawler! It was blocked by super class.
	 * @overwrites {ToolBarBinding#crawlerFilters}
	 * @type {List<string>}
	 */
	this.crawlerFilters	= new List ([ FlexBoxCrawler.ID ]);
	
	/*
	 * Returnable.
	 */
	return this;
}

/**
 * Identifies binding.
 */
DialogToolBarBinding.prototype.toString = function () {
	
	return "[DialogToolBarBinding]";
}

/**
 * @overloads {ToolBarBinding#onBindingRegister}
 */
DialogToolBarBinding.prototype.onBindingRegister = function () {
	
	DialogToolBarBinding.superclass.onBindingRegister.call ( this );
	
	this.addActionListener ( Binding.ACTION_FOCUSED );
	this.addActionListener ( Binding.ACTION_BLURRED );
}

/**
 * @overloads {ToolBarBinding#onBindingDispose}
 */
DialogToolBarBinding.prototype.onBindingDispose = function () {

	DialogToolBarBinding.superclass.onBindingDispose.call ( this );
	if ( this._isListening == true ) {
		this.unsubscribe ( BroadcastMessages.KEY_ENTER );
	}
}

/**
 * @overloads {ToolBarBinding#onBindingInitialize}
 */
DialogToolBarBinding.prototype.onBindingInitialize = function () {
	
	this.indexDialogButtons ();
	DialogToolBarBinding.superclass.onBindingInitialize.call ( this );
}

/**
 * Index dialog buttons. Invoke this manually when adding new buttons.
 */
DialogToolBarBinding.prototype.indexDialogButtons = function () {

	var buttons = this.getDescendantBindingsByLocalName ( "clickbutton" );
	
	if ( buttons.hasEntries ()) {
		while ( buttons.hasNext ()) {
			var button = buttons.getNext ();
			if ( button.isDefault ) {
				this._defaultButton = button;
				button.attachClassName ( ButtonBinding.CLASSNAME_DEFAULT );
			}
			if ( !this._isListening && button.isFocusable ) {
				this.subscribe ( BroadcastMessages.KEY_ENTER );
				this._isListening = true;
			}
		}
		this._buttons = buttons;
	}
}

/**
 * @implements {IBroadcastListener}
 * @param {string} broadcast
 * @param {object} arg
 */
DialogToolBarBinding.prototype.handleBroadcast = function ( broadcast, arg ) {
	
	DialogToolBarBinding.superclass.handleBroadcast.call ( this, broadcast, arg );
	
	switch ( broadcast ) {
		case BroadcastMessages.KEY_ENTER :
			
			/*
			 * Don't close the dialog while a popup is handled!
			 */
			if ( !PopupBinding.hasActiveInstances () && !EditorBinding.isActive ) {
				
				/*
				 * Only close active dialogs (don't close a chain of dialogs)!
				 */
				if ( Binding.exists ( this )) {
					var dialog = this.getAncestorBindingByType ( DialogBinding, true );
					if ( dialog != null && dialog.isActive ) {
						if ( this._focusedButton != null ) {
							if ( !this._focusedButton.isDisabled ) {
								this.unsubscribe ( BroadcastMessages.KEY_ENTER );
								this._focusedButton.fireCommand ();
							}
						} else {
							if ( !this._defaultButton.isDisabled ) {
								this.unsubscribe ( BroadcastMessages.KEY_ENTER );
								this._defaultButton.fireCommand ();
							}
						}
					}
				} else {
					this.logger.error ( "Ouch: DialogToolBarBinding#handleBroadcast" );
				}
			}
			break;
	}
}

/**
 * @implements {IActionListener}
 * @overloads {Binding#handleAction}
 * @param {Action} action
 */
DialogToolBarBinding.prototype.handleAction = function ( action ) {
	
	DialogToolBarBinding.superclass.handleAction.call ( this, action );
	
	var binding = action.target;
	var isFocused = false;
	var buttons = this._buttons.reset ();
	
	if ( binding instanceof ClickButtonBinding ) {
		switch ( action.type ) {
			case Binding.ACTION_FOCUSED :
				binding.attachClassName ( ButtonBinding.CLASSNAME_FOCUSED );
				this._focusedButton = binding;
				if ( this._defaultButton ) {
					this._defaultButton.detachClassName ( ButtonBinding.CLASSNAME_DEFAULT );
				}
				break;
			case Binding.ACTION_BLURRED :
				binding.detachClassName ( ButtonBinding.CLASSNAME_FOCUSED );
				break;
		}
	}
	
	/*
	 * When focus leaves the toolbar, restore default focus.
	 */
	if ( this._defaultButton ) {
		while ( !isFocused && buttons.hasNext ()) {
			var button = buttons.getNext ();
			isFocused = button.isFocused;
		}
		if ( !isFocused ) {
			this._defaultButton.attachClassName ( ButtonBinding.CLASSNAME_DEFAULT );
			this._focusedButton = null;
		}
	}
}